package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.entity.Quarter;
import com.xmy.cultivate.entity.SchoolQuarter;
import com.xmy.cultivate.entity.SenateScore;
import com.xmy.cultivate.entity.response.PracticePlanStaff;
import com.xmy.cultivate.service.IQuarterService;
import com.xmy.cultivate.service.ISchoolQuarterService;
import com.xmy.cultivate.service.ISenateDepartProjectsService;
import com.xmy.cultivate.service.ISenateScoreService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 教务积分 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2024-10-26
 */
@RestController
@RequestMapping("/api/senateScore")
public class SenateScoreController {

    @Autowired
    ISenateScoreService iSenateScoreService;

    @Autowired
    IQuarterService iQuarterService;

    @Autowired
    ISchoolQuarterService iSchoolQuarterService;

    @Autowired
    ISenateDepartProjectsService iSenateDepartProjectsService;

    /**
     * 教务积分列表
     */
    @GetMapping("/getSenateScoreList")
    public Result getSenateScoreList(@RequestParam(value = "yearPart", required = false) Integer yearPart,
                                     @RequestParam(value = "quarterNum", required = false) Integer quarterNum,
                                     @RequestParam(value = "startLessonNum", required = false) String startLessonNum,// 开始课次
                                     @RequestParam(value = "endLessonNum", required = false) String endLessonNum,// 结束课次
                                     @RequestParam(value = "subjectsId", required = false) String subjectsId,// 科目
                                     @RequestParam(value = "projectsId", required = false) String projectsId,// 项目id
                                     @RequestParam(value = "senateDepartId", required = false) String senateDepartId,// 部门id
                                     @RequestParam(value = "startDate", required = false) String startDate,// 开始日期
                                     @RequestParam(value = "endDate", required = false) String endDate,// 结束日期
                                     @RequestParam(value = "type", required = false) String type,
                                     @RequestParam(value = "schoolId", required = false) String schoolId,
                                     @RequestParam(value = "yearClassId", required = false) String yearClassId,
                                     @RequestParam(value = "teacherName", required = false) String teacherName,
                                     @RequestParam(value = "trackType", required = false, defaultValue = "") String trackType,
                                     @RequestParam(value = "teamId", required = false, defaultValue = "") String teamId) {
        if (yearPart == null || quarterNum == null) {
            Quarter quarterNow = iQuarterService.getNowQuarter();
            yearPart = quarterNow.getYearPart();
            quarterNum = quarterNow.getNum();
        }

        // 如果项目id为空（全部），部门id不为空，根据部门获取项目id并拼接
        if (StringUtils.isBlank(projectsId) && StringUtils.isNotBlank(senateDepartId)) {
            projectsId = iSenateDepartProjectsService.getProjectsIdStr(senateDepartId);
        }

        List<SenateScore> senateScoreList = new ArrayList<>();

        if (type.equals("5")) {
            senateScoreList = iSenateScoreService.getSenateScoreTeamList(yearPart, quarterNum, startLessonNum, endLessonNum, subjectsId, projectsId, startDate, endDate, senateDepartId, trackType, teamId, yearClassId);
        } else {
            senateScoreList = iSenateScoreService.getSenateScoreList(yearPart, quarterNum, startLessonNum, endLessonNum, subjectsId, projectsId, startDate, endDate, schoolId, teacherName, type, yearClassId, senateDepartId);

            if (type.equals("6") && StringUtils.isBlank(schoolId)) {
                List<SenateScore> collectList = new ArrayList<>();

                QueryWrapper queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("year_part", yearPart);
                queryWrapper.eq("quarter_num", quarterNum);
                queryWrapper.eq("pid", 0);
                List<SchoolQuarter> schoolQuarterList = iSchoolQuarterService.list(queryWrapper);

                for (SchoolQuarter schoolQuarter : schoolQuarterList) {
                    SenateScore senateScore = new SenateScore();
                    senateScore.setSchoolId(schoolQuarter.getSchoolId());
                    senateScore.setSchoolName(schoolQuarter.getSchoolName());
                    senateScore.setSchoolPid(schoolQuarter.getPid());
                    senateScore.setAllScore(0);
                    senateScore.setDateScore(0);
                    senateScore.setTeacherCount(0);
                    collectList.add(senateScore);
                }

                for (SenateScore senateScore : collectList) {
                    List<SenateScore> childRates = new ArrayList<>();
                    for (SenateScore score : senateScoreList) {
                        if (senateScore.getSchoolId().equals(score.getSchoolId()) || senateScore.getSchoolId().equals(score.getSchoolPid())) {
                            childRates.add(score);
                            if (score.getAllScore() != null) {
                                senateScore.setAllScore(senateScore.getAllScore() + score.getAllScore());
                            }
                            if (score.getDateScore() != null) {
                                senateScore.setDateScore(senateScore.getDateScore() + score.getDateScore());
                            }
                            if (score.getTeacherCount() != null) {
                                senateScore.setTeacherCount(senateScore.getTeacherCount() + score.getTeacherCount());
                            }
                        }
                    }
                    senateScore.setChildRateList(childRates);
                }
                senateScoreList = collectList;
            }

        }

        return new Result(Code.OK, new PageResult(senateScoreList, (long) senateScoreList.size(), 1L, ""), "");
    }

    @GetMapping("/getSenateScoreDetail")
    public Result getSenateScoreDetail(@RequestParam(value = "yearPart", required = false) Integer yearPart,
                                       @RequestParam(value = "quarterNum", required = false) Integer quarterNum,
                                       @RequestParam(value = "startLessonNum", required = false) Integer startLessonNum,
                                       @RequestParam(value = "endLessonNum", required = false) Integer endLessonNum,
                                       @RequestParam(value = "subjectsId", required = false) String subjectsId,
                                       @RequestParam(value = "projectsId", required = false) String projectsId,
                                       @RequestParam(value = "senateDepartId", required = false) String senateDepartId,// 部门id
                                       @RequestParam(value = "startDate", required = false) String startDate,
                                       @RequestParam(value = "endDate", required = false) String endDate,
                                       @RequestParam(value = "teacherId", required = false) Long teacherId,
                                       @RequestParam(value = "detailType", required = false) Integer detailType,
                                       @RequestParam(value = "type", required = false) String type,
                                       @RequestParam(value = "schoolId", required = false) String schoolId,
                                       @RequestParam(value = "yearClassId", required = false) String yearClassId,
                                       @RequestParam(value = "teacherName", required = false) String teacherName,
                                       @RequestParam(value = "trackType", required = false, defaultValue = "") String trackType,
                                       @RequestParam(value = "teamId", required = false, defaultValue = "") String teamId) {
        if (yearPart == null || quarterNum == null) {
            Quarter quarterNow = iQuarterService.getNowQuarter();
            yearPart = quarterNow.getYearPart();
            quarterNum = quarterNow.getNum();
        }

        // 如果项目id为空（全部），部门id不为空，根据部门获取项目id并拼接
        if (StringUtils.isBlank(projectsId) && StringUtils.isNotBlank(senateDepartId)) {
            projectsId = iSenateDepartProjectsService.getProjectsIdStr(senateDepartId);
        }

        List<SenateScore> senateScoreList = new ArrayList<>();
        if (type.equals("5")) {
            senateScoreList = iSenateScoreService.getSenateScoreTeamDetail(yearPart, quarterNum, startLessonNum, endLessonNum, teacherId, subjectsId, projectsId, detailType, startDate, endDate, senateDepartId, trackType, teamId, yearClassId);
        } else {
            senateScoreList = iSenateScoreService.getSenateScoreDetail(yearPart, quarterNum, startLessonNum, endLessonNum, teacherId, subjectsId, projectsId, detailType, startDate, endDate, schoolId, teacherName, yearClassId, senateDepartId);
        }

        return new Result(Code.OK, new PageResult(senateScoreList, (long) senateScoreList.size(), 1L, ""), "");
    }

}
